﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Super.DomainService;
using Super.Common;
using Super.Filter;
using Super.Entities;
using System.Web.Script.Serialization;

namespace Super.Web.Controllers
{
    //[BKActionFilter]
    //[BkExceptionFilter]
    public class BaseController : CommonController
    {
        /// <summary>
        /// 默认分页大小。 
        /// </summary>
        protected int PageSize = 15;

        public BaseController()
        {
            ViewBag.UserID = CookieService.GetUserID();
            ViewBag.UID = CookieService.GetUID();
            ViewBag.UserName = CookieService.GetUserName();
        }

        public string PageValidateMessage
        {
            set { ViewData["PageValidateMessage"] = value; }
        }

        /// <summary>
        /// 绑定分页。
        /// </summary>
        /// <param name="pageIndex">当前页码。</param>
        /// <param name="pageSize">分页大小。</param>
        /// <param name="itemCount">记录总条数[默认为PageSize条]。</param>
        /// <remarks>zhourq@20110906</remarks>
        protected void BindPage(int itemCount, int pageIndex, int pageSize)
        {
            ViewBag.PageSize = pageSize;
            ViewBag.PageIndex = pageIndex;
            ViewBag.RecordCount = itemCount;
        }

        /// <summary>
        /// 在对列表进行增删改的时候获取操作后的当前页码。
        /// </summary>
        /// <param name="reduceCount">记录减少的数量。</param>
        /// <remarks>zhourq@20110906</remarks>
        public int GetCurrentPage(int? reduceCount = 0)
        {
            var pageIndex = Query.Get("pageindex").ToInt();
            var pageSize = Query.Get("pagesize").ToInt();
            var itemCount = Query.Get("itemcount").ToInt() - reduceCount;

            var pageCount = itemCount / pageSize;
            var remainder = itemCount % pageSize;

            if (pageCount != pageIndex && remainder == 0)
            {
                return pageIndex == 1 ? 1 : pageIndex - 1;
            }

            return pageIndex;
        }


        /// <summary>
        /// 页面的建议Tip
        /// </summary>
        /// <param name="message">提示的消息</param>
        /// <param name="isParent">是否父页面</param>
        /// <param name="type">可选的值：'info'、'success'、'warning'、'error'、'loading'</param>
        /// <param name="time">消失的时间 </param>
        public void PageTip(string message, bool isParent = false, string type = "success", int time = 800)
        {
            var tip = "";
            if (isParent)
            {
                tip = JavaScriptFormat(" parent.$.jBox.tip('" + message + "','" + type + "',{ timeout:" + time + " }); parent.$.jBox.closeTip(true);  parent.location.reload();");

            }
            else
            {
                tip = JavaScriptFormat(" $.jBox.tip('" + message + "','" + type + "',{ timeout:" + time + " });");

            }


            TempData["PageMessage"] = tip;
        }

        /// <summary>
        /// 操作的提示消息 需要用户点关闭
        /// </summary>
        /// <param name="message">页面提示消息</param>
        public void PageInfo(string message)
        {
            var info = JavaScriptFormat("$.jBox.info('" + message + "');");
            TempData["PageMessage"] = info;
        }

        /// <summary>
        /// 封装返回js格式
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        public static string JavaScriptFormat(string code)
        {
            return (string.IsNullOrEmpty(code))
                       ? ""
                       : string.Format("<script type='text/javascript' language='javascript'>{0}</script>", code);
        }
    }
}
